package com.feilong.edgeAI.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;

import com.aiibt.common.core.WDActivity;
import com.aiibt.common.util.Constant;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.feilong.edgeAI.R;
import com.feilong.edgeAI.databinding.ActivityAwakeBinding;
import com.feilong.edgeAI.model.ResetAwakeWords;
import com.feilong.edgeAI.speech.IvwHelper;
import com.feilong.edgeAI.utils.TextUtil;
import com.feilong.edgeAI.vm.AwakeViewModel;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by【仿脑科技】
 *更改唤醒词界面
 * @Author: potato
 * @CreateDate: 2023/12/25 17:43
 */
@Route(path = Constant.ACTIVITY_URL_AWAKE)
public class AwakeActivity extends WDActivity<AwakeViewModel, ActivityAwakeBinding> {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_awake;
    }

    private String keyword;

    @Override
    protected void initView(Bundle savedInstanceState) {

        String keyPath = IvwHelper.getKeyPath();
        keyword = FileIOUtils.readFile2String(keyPath,"GB2312");
        if (!TextUtils.isEmpty(keyword)) {
            if(keyword.contains(";")){
                String arr[] = keyword.split(";");
                keyword = arr[0];
            }
            if(keyword.contains("\r\n")){
                keyword = keyword.substring(0,keyword.indexOf("\r\n"));
            }
            binding.editText.setText(keyword);
        }
        viewModel.saveData.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
//                viewModel.save();
////                EventBus.getDefault().post(new ModifyKeyWord());
//                Toast.makeText(AwakeActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
//                finish();
            }
        });
        binding.confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String word = binding.editText.getText().toString();

                if (!TextUtils.isEmpty(word)) {
                    if(word.contains("\r\n")){
                        word = word.substring(0,word.indexOf("\r\n"));
                    }
                        String keyPath = IvwHelper.getKeyPath();
//                    FileIOUtils.writeFileFromString(keyPath, word);
                        TextUtil.saveTxt(keyPath,word);
//            saveData.setValue(true);
                        EventBus.getDefault().post(new ResetAwakeWords(word));
                    ToastUtils.showShort(R.string.modify_success);
                    finish();
                }
            }
        });
    }


}
